<?php

namespace app\admin\model\article;


use think\Model;
use think\Request;
/**
 * 文件库模型
 * Class UploadFile
 * @package app\common\model
 */
class UploadFile extends Model
{
    protected $name = 'upload_file';
    protected $updateTime = false;
    protected $deleteTime = false;
    protected $append = ['file_path','back_img'];
    public static $base_url;
    /**
     * 获取列表记录
     * @param int $groupId 分组id
     * @param string $fileType 文件类型
     * @param bool|int $isRecycle
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($id,$groupId = -1, $fileType = '', $isRecycle = -1)
    {
        // 查询列表数据
        if($groupId==-1){
            $where=['is_user' => 0, 'is_delete' => 0,'userid'=>$id];
        }else{
            $where=['is_user' => 0, 'is_delete' => 0,'userid'=>$id,'group_id'=>$groupId];
        }
        return $this->where($where)
            ->order(['file_id' => 'desc'])
            ->paginate(30, false, [
                'query' => Request::instance()->request()
            ]);
    }

    /**
     * 获取图片完整路径
     * @param $value
     * @param $data
     * @return string
     */
    public function getBackImgAttr($value, $data)
    {

        if ($data['file_type'] === 'video') {
            return "https://img.aishenghuolife.cn/%E8%A5%BF%E5%AE%89%E7%88%B1%E7%94%9F%E6%B4%BB%E6%88%BF%E5%9C%B0%E4%BA%A7%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8/5ad842bb90d8667a28be2d15ab588632bd8ae0ee.png";
        }elseif($data['file_type'] === 'image/jpg'|| $data['file_type'] === 'image/jpeg'||$data['file_type'] === 'image/png'){
            return $data['file_url']."/".$data['file_name'];
        }else{
            return "https://img.aishenghuolife.cn/%E8%A5%BF%E5%AE%89%E7%88%B1%E7%94%9F%E6%B4%BB%E6%88%BF%E5%9C%B0%E4%BA%A7%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8/5ad842bb90d8667a28be2d15ab588632bd8ae0ee.png";
        }
        return $value;

    }

    /**
     * 移入|移出回收站
     * @param bool $isRecycle
     * @return false|int
     */
    public function setRecycle($isRecycle = true)
    {
        return $this->save(['is_recycle' => (int)$isRecycle]);
    }

    /**
     * 删除文件
     * @return false|int
     * @throws \think\Exception
     */
    public function setDelete()
    {
        // 存储配置信息
        $config = SettingModel::getItem('storage');
        // 实例化存储驱动
        $StorageDriver = new StorageDriver($config, $this['storage']);
        // 删除文件
        if (!$StorageDriver->delete($this['file_name'])) {
            $this->error = '文件删除失败：' . $StorageDriver->getError();
            return false;
        }
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 批量软删除
     * @param $fileIds
     * @return $this
     */
    public function softDelete($fileIds)
    {
        return $this->where('file_id', 'in', $fileIds)->update(['is_delete' => 1]);
    }

    /**
     * 批量移动文件分组
     * @param $group_id
     * @param $fileIds
     * @return $this
     */
    public function moveGroup($group_id, $fileIds)
    {
        return $this->where('file_id', 'in', $fileIds)->update(compact('group_id'));
    }

    /**
     * 关联文件库分组表
     * @return \think\model\relation\BelongsTo
     */
    public function uploadGroup()
    {
        return $this->belongsTo('UploadGroup', 'group_id');
    }

    /**
     * 获取图片完整路径
     * @param $value
     * @param $data
     * @return string
     */
    public function getFilePathAttr($value, $data)
    {
        if ($data['storage'] === 'local') {
            return self::$base_url . 'uploads/' . $data['file_name'];
        }
        return $data['file_url'] . '/' . $data['file_name'];
    }

    /**
     * 文件详情
     * @param $file_id
     * @return null|static
     * @throws \think\exception\DbException
     */
    public static function detail($file_id)
    {
        return self::get($file_id);
    }

    /**
     * 根据文件名查询文件id
     * @param $fileName
     * @return mixed
     */
    public static function getFildIdByName($fileName)
    {
        return (new static)->where(['file_name' => $fileName])->value('file_id');
    }

    /**
     * 查询文件id
     * @param $fileId
     * @return mixed
     */
    public static function getFileName($fileId)
    {
        return (new static)->where(['file_id' => $fileId])->value('file_name');
    }

    /**
     * 添加新记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        return $this->save($data);
    }

}
